Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
Javascript "multidimensional" array behaving differently based on location in script

I've created a "multidimensional" JS array with a function. Within that function, the console log shows the array with all elements as I would expect it.

Outside of the function, I can still show the array in the console, but it looks different, and I can't access elements with the typical arr[0] identifiers.

Any ideas what might be going on? I need to be able to access this array globally. Please be patient with me, I'm new and green.

var thisQsetTrue = [];
var thisQsetFalse = [];
//Start getDoc
function getDoc() {
    const xhttp = new XMLHttpRequest();
    xhttp.onload = function() {
        buildArray(this);
    }
    xhttp.open("GET", "AddisonNovice.xml");
    xhttp.send();
}
//End get Doc
//Start buildArray
function buildArray(xml) {
    const xmlDoc2 = xml.responseXML;
    const y = xmlDoc2.getElementsByTagName("question");
    //Build the true answer array
    for (let i = 0; i <y.length; i++) {
        var tempArray = [];
        if (y[i].getElementsByTagName("type")[0].childNodes[0].nodeValue == "true") {
            tempArray.push(y[i].getElementsByTagName("query")[0].childNodes[0].nodeValue);
            tempArray.push(y[i].getElementsByTagName("feedback")[0].childNodes[0].nodeValue);
            thisQsetTrue.push(tempArray);
        }
    }
    //Build the false answer array
    for (let i = 0; i <y.length; i++) {
        var tempArray2 = [];
        if (y[i].getElementsByTagName("type")[0].childNodes[0].nodeValue == "false") {
            tempArray2.push(y[i].getElementsByTagName("query")[0].childNodes[0].nodeValue);
            tempArray2.push(y[i].getElementsByTagName("feedback")[0].childNodes[0].nodeValue);
            thisQsetFalse.push(tempArray2);
        }
    }
    console.log(thisQsetFalse);//Array displays normally
}
//End buildArray
//Call getDoc
getDoc();
console.log(thisQsetFalse);//Array displays differently

Console log screenshot. As you can see, all the elements of the array are there, but it looks different, and I can't get to the elements: Screenshot of console log

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda